package com.simtek.simplayer.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.webkit.URLUtil;

public class StringUtils {

	public static boolean isBlank(String string) {
		if (string == null)
			return true;
		return string.length() == 0;
	}
	
	public static boolean isNotBlank(String string) {
		return !StringUtils.isBlank(string);
	}

	public static boolean equals(String s1, String s2) {
		if (s1 == s2) {
			return true;
		}

		if (s1 == null || s2 == null) {
			return false;
		}

		return s1.equals(s2);
	}

	public static boolean equalsIgnoreCase(String s1, String s2) {
		if (s1 == s2) {
			return true;
		}

		if (s1 == null || s2 == null) {
			return false;
		}

		return s1.equalsIgnoreCase(s2);
	}

	public static String downloadUrl(String myurl) throws IOException {
		InputStream is = null;
		String res = null;
		try {
			URL url = new URL(myurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(10000 /* milliseconds */);
			conn.setConnectTimeout(15000 /* milliseconds */);
			conn.setRequestMethod("GET");
			conn.setDoInput(true);
			// Starts the query
			conn.connect();
			int response = conn.getResponseCode();
			Log.d("StringUtils", "The response is: " + response);
			is = conn.getInputStream();

			StringWriter writer = new StringWriter();
			IOUtils.copy(is, writer, Charset.defaultCharset());

			res = writer.toString();
		} finally {
			if (is != null) {
				is.close();
			}
		}
		return res;
	}

	public static List<String> getHttpLink(String input) {
		Pattern httpPattern = Pattern.compile(
				"(https?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])",
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = httpPattern.matcher(input);

		List<String> res = null;
		while (matcher.find()) {
			if (res == null) {
				res = new ArrayList<String>();
			}
			res.add(matcher.group(0));
		}
		return res;
	}

	public static String getVideoPathByUri(Context context, Uri uri) {
		String uriString = uri.toString();
		if (URLUtil.isContentUrl(uriString)) {
			String[] projection = { MediaStore.Video.Media.DATA };
			Cursor cursor = context.getContentResolver().query(uri, projection,
					null, null, null);
			if (cursor != null && cursor.moveToFirst()) {
				String path = cursor.getString(0);
				cursor.close();
				return path;
			}
		} else if (URLUtil.isFileUrl(uriString)) {
			return uri.getPath();
		} else if (URLUtil.isHttpsUrl(uriString)
				|| URLUtil.isHttpUrl(uriString)) {
			return uriString;
		}
		return null;
	}

	public static String getImagePathByUri(Context context, Uri uri) {
		String uriString = uri.toString();
		if (URLUtil.isContentUrl(uriString)) {
			String[] projection = { MediaStore.Images.Media.DATA };
			Cursor cursor = context.getContentResolver().query(uri, projection,
					null, null, null);
			if (cursor != null && cursor.moveToFirst()) {
				String path = cursor.getString(0);
				cursor.close();
				return path;
			}
		} else if (URLUtil.isFileUrl(uriString)) {
			return uri.getPath();
		} else if (URLUtil.isHttpsUrl(uriString)
				|| URLUtil.isHttpUrl(uriString)) {
			return uriString;
		}
		return null;
	}

	/**
	 * @param context
	 * @param uri
	 * @return res[] : res[0] = url, res[1]= title;
	 */
	public static String[] getVideoInfoByUri(Context context, Uri uri) {
		try {
			String[] projection = { MediaStore.Video.Media.DATA,
					MediaStore.Video.Media.TITLE };
			Cursor cursor = context.getContentResolver().query(uri, projection,
					null, null, null);
			if (cursor == null)
				return null;
			cursor.moveToFirst();
			String[] res = new String[] { cursor.getString(0),
					cursor.getString(1) };
			cursor.close();
			return res;

		} catch (Throwable e) {
			e.printStackTrace();
		}
		return null;
	}
}
